package top.riske.gen3d.controller;

import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.web.bind.annotation.*;
import top.riske.gen3d.DTO.AccountDto;
import top.riske.gen3d.common.DTO.JwtDto;
import top.riske.gen3d.entity.Account;
import top.riske.gen3d.http.request.AccountRequest;
import top.riske.gen3d.http.response.Result;
import top.riske.gen3d.service.AccountService;

@RestController
@RequestMapping("/api")
public class AccountController {

    @Resource
    private AccountService accountService;

    /**
     * 登录
     * @param accountRequest
     * @param response
     * @return
     */
    @PostMapping("/login")
    public Result<AccountDto> login(@RequestBody AccountRequest accountRequest, HttpServletResponse response) {
        return Result.success(accountService.login(accountRequest.getUsername(), accountRequest.getPassword(), response).toDto());
    }

    /**
     *  注册
     * @param accountRequest
     * @param response
     * @return
     */
    @PostMapping("/register")
    public Result<AccountDto> register(@RequestBody AccountRequest accountRequest, HttpServletResponse response) {
        return Result.success(accountService.register(accountRequest.getUsername(), accountRequest.getPassword(),response).toDto());
    }

    /**
     * 获取用户信息
     * @param jwtDto
     * @return
     */
    @GetMapping("/protected/user/getUserInfo")
    public Result<AccountDto> getUserInfo(JwtDto jwtDto){
        return Result.success(accountService.getById(jwtDto.getUid()).toDto());
    }

    @PostMapping("/protected/user/updateUserInfo")
    public Result<AccountDto> updateUserInfo(@RequestBody Account account, JwtDto jwtDto){
        // 更新用户信息
        accountService.updateById(account);
        return Result.success(account.toDto());
    }

}
